﻿using _03Net8Logging.Application;
using _03Net8Logging.Customs;
using Karambolo.Extensions.Logging.File;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.VisualBasic;
using System.Runtime.CompilerServices;

namespace _03Net8Logging
{
    internal class Program
    {
        static void Main(string[] args)
        {
            #region 日志输出到控制台
            {
                //// 1、创建日志对象ILogger
                //Console.WriteLine("Hello, World!");

                //// 2、创建日志对象ILogger
                //ILoggerFactory loggerFactory = LoggerFactory.Create(builer =>
                //{
                //    // 2.1 配置Console日志
                //    builer.AddConsole();
                //});

                //// 2.2 创建ILogger 接口
                //ILogger<Program> logger = loggerFactory.CreateLogger<Program>();

                //// 2.3 输出日志
                //logger.LogInformation("Hello, Logging");
                //logger.LogError("Hello,ErrorLogging");
                //logger.LogError("Hello,ErrorLogging2");
            }
            #endregion


            #region 2、 日志输出到文件
            //{
            //    // 1、创建日志对象ILogger
            //    Console.WriteLine("Hello, World!");

            //    // 2、创建日志对象ILogger
            //    ILoggerFactory loggerFactory = LoggerFactory.Create(builer =>
            //    {
            //        // 2.1 配置文件日志
            //        builer.AddFile(options =>
            //        {
            //            // 1\ 配置文件目录
            //            options.RootPath = AppContext.BaseDirectory;
            //            options.Files = new[]
            //            {
            //                new LogFileOptions
            //                {
            //                    Path = "/logs/log.txt"
            //                }
            //            };

            //        });
            //    });

            //    // 2.2 创建ILogger 接口
            //    ILogger<Program> logger = loggerFactory.CreateLogger<Program>();

            //    // 2.3 输出日志
            //    logger.LogInformation("Hello, Logging");
            //    Console.ReadKey();
            //}
            #endregion

            #region 3、 自定义日志输出 - MYSQL
            //{
            //    // 1、创建日志对象ILogger
            //    Console.WriteLine("Hello, World!");
            //    var connectionString = "server=localhost;database=fu;user=root;password=root;";
            //    // 2、创建日志对象ILogger
            //    ILoggerFactory loggerFactory = LoggerFactory.Create(builer =>
            //    {
            //        builer.AddConsole();
            //        // 2.1 配置文件日志
            //        builer.AddFile(options =>
            //        {
            //            // 1\ 配置文件目录
            //            options.RootPath = AppContext.BaseDirectory;
            //            options.Files = new[]
            //            {
            //                new LogFileOptions
            //                {
            //                    Path = "/logs/log.txt"
            //                }
            //            };

            //        });

            //        //builer.AddProvider(new MySQLLoggerProvider(connectionString));
            //        builer.AddMySQL(connectionString);
            //    });

            //    // 2.2 创建ILogger 接口
            //    ILogger<Program> logger = loggerFactory.CreateLogger<Program>();

            //    // 2.3 输出日志
            //    logger.LogInformation("Hello, Logging");
            //}
            #endregion

            #region 4、 自定义日志输出 - MYSQL - 级别
            //{
            //    // 1、创建日志对象ILogger
            //    Console.WriteLine("Hello, World!");
            //    var connectionString = "server=localhost;database=fu;user=root;password=root;";
            //    // 2、创建日志对象ILogger
            //    ILoggerFactory loggerFactory = LoggerFactory.Create(builer =>
            //    {
            //        builer.AddConsole().SetMinimumLevel(LogLevel.Trace);
            //        // 2.1 配置文件日志
            //        builer.AddFile(options =>
            //        {
            //            // 1\ 配置文件目录
            //            options.RootPath = AppContext.BaseDirectory;
            //            options.Files = new[]
            //            {
            //                new LogFileOptions
            //                {
            //                    Path = "/logs/log.txt"
            //                }
            //            };

            //        });

            //        //builer.AddProvider(new MySQLLoggerProvider(connectionString));
            //        builer.AddMySQL(connectionString);
            //    });

            //    // 2.2 创建ILogger 接口
            //    ILogger<Program> logger = loggerFactory.CreateLogger<Program>();

            //    // 2.3 输出日志
            //    logger.LogTrace("Hello, Logging");
            //    logger.LogDebug("Hello, Logging");
            //    logger.LogInformation("Hello, Logging");
            //    logger.LogWarning("Hello, Logging");
            //    logger.LogError("Hello, Logging");
            //    logger.LogCritical("Hello, Logging");
            //}
            #endregion

            #region 5、 自定义日志输出 - MYSQL - 格式化
            {
                //// 1、创建日志对象ILogger
                //Console.WriteLine("Hello, World!");
                //var connectionString = "server=localhost;database=fu;user=root;password=root;";
                //// 2、创建日志对象ILogger
                //ILoggerFactory loggerFactory = LoggerFactory.Create(builer =>
                //{
                //    builer.AddConsole().SetMinimumLevel(LogLevel.Trace);
                //    // 2.1 配置文件日志
                //    builer.AddFile(options =>
                //    {
                //        // 1\ 配置文件目录
                //        options.RootPath = AppContext.BaseDirectory;
                //        options.Files = new[]
                //        {
                //            new LogFileOptions
                //            {
                //                Path = "/logs/log.txt"
                //            }
                //        };

                //    });

                //    //builer.AddProvider(new MySQLLoggerProvider(connectionString));
                //    builer.AddMySQL(connectionString);
                //});

                //// 2.2 创建ILogger 接口
                //ILogger<Program> logger = loggerFactory.CreateLogger<Program>();

                //// 2.3 输出日志
                ////logger.LogTrace("Hello, Logging");
                ////logger.LogDebug("Hello, Logging");
                ////logger.LogInformation("Hello, Logging");
                ////logger.LogWarning("Hello, Logging");
                ////logger.LogError("Hello, Logging");
                ////logger.LogCritical("Hello, Logging");
                //LogDebug(logger, "LogDebug");
                //LogInformation(logger, "LogInformation");
                //LogWarning(logger, "LogWarning");
                //LogError(logger, "LogError");
                //LogCritical(logger, "LogCritical");
            }
            #endregion

            #region 6、日志输出应用
            //{
            //    var connectionString = "server=localhost;database=fu;user=root;password=root;";
            //    // 1、创建日志对象ILogger
            //    ServiceCollection services = new ServiceCollection();
            //    services.AddLogging(a =>
            //    {
            //        a.AddConsole().SetMinimumLevel(LogLevel.Error);
            //        a.AddFile(options =>
            //        {
            //            // 1\ 配置文件目录
            //            options.RootPath = AppContext.BaseDirectory;
            //            options.Files = new[]
            //            {
            //                    new LogFileOptions
            //                    {
            //                        Path = "/logs/log.txt"
            //                    }
            //            };

            //        }).SetMinimumLevel(LogLevel.Information);
            //        a.AddMySQL(connectionString);

            //    });
            //    services.AddSingleton<OrderService>();

            //    var provider = services.BuildServiceProvider();

            //    OrderService order = provider.GetRequiredService<OrderService>();
            //    order.GetOrder();
            //}
            #endregion


            #region 7、日志输出应用- 配置文件
            {
                var connectionString = "server=localhost;database=fu;user=root;password=root;";

                ConfigurationBuilder builder = new ConfigurationBuilder();
                builder.AddJsonFile("AppSettings.json");
                IConfiguration configuration = builder.Build();

                // 1、创建日志对象ILogger
                ServiceCollection services = new ServiceCollection();
                services.AddLogging(a =>
                {
                    a.AddConfiguration(configuration.GetSection("Logging"));
                    a.AddConsole();
                    a.AddFile(options =>
                    {
                        // 1\ 配置文件目录
                        options.RootPath = AppContext.BaseDirectory;
                        options.Files = new[]
                        {
                                new LogFileOptions
                                {
                                    Path = "/logs/log.txt"
                                }
                        };

                    });
                    a.AddMySQL(connectionString);

                });
                services.AddSingleton<OrderService>();

                var provider = services.BuildServiceProvider();

                OrderService order = provider.GetRequiredService<OrderService>();
                order.GetOrder();
            }
            #endregion
        }

        // 日志记录方法，包括调用者信息
        public static void LogDebug(ILogger logger, string message, [CallerMemberName] string memberName = "", [CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0)
        {
            logger.LogDebug($"[{memberName} in {filePath}:{lineNumber}] {message}");
        }

        public static void LogInformation(ILogger logger, string message, [CallerMemberName] string memberName = "", [CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0)
        {
            logger.LogInformation($"[{memberName} in {filePath}:{lineNumber}] {message}");
        }

        public static void LogWarning(ILogger logger, string message, [CallerMemberName] string memberName = "", [CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0)
        {
            logger.LogWarning($"[{memberName} in {filePath}:{lineNumber}] {message}");
        }

        public static void LogError(ILogger logger, string message, [CallerMemberName] string memberName = "", [CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0)
        {
            logger.LogError($"[{memberName} in {filePath}:{lineNumber}] {message}");
        }

        public static void LogCritical(ILogger logger, string message, [CallerMemberName] string memberName = "", [CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0)
        {
            logger.LogCritical($"[{memberName} in {filePath}:{lineNumber}] {message}");
        }
    
    }
}
